package com.cndw.kungfu.event;

import java.util.concurrent.ScheduledFuture;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsSystem;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.rpg.framework.ResponseMessage;

public class GamePowerNoticeHandler extends BaseHandler implements Runnable {

	private static int times = 0;
	private int smallRadio = 0;
	
	private static String colseMsg= null;
	
	private static ScheduledFuture<?> smallFuture;

	public void reset() {
		times = 0;
		smallRadio = 0;
	}	

	public static String getColseMsg() {
		return colseMsg;
	}
	public static void setColseMsg(String colseMsg) {
		GamePowerNoticeHandler.colseMsg = colseMsg;
	}

	public static void close(){
		if (smallFuture != null) {
			smallFuture.cancel(false);
		}
	}


	@Override
	public void run() {
		if (smallRadio == 1) {
			return;
		}
		int delayTime = ConstantsSystem.getPowerCloseTime() - TimeProcesser.getUnixTime();
		if (delayTime <= 0) {
			return;
		}
		String msg = LanguageLib.get("comm.sys.poweroff", delayTime);
		
		if (times == 0) {
			if (colseMsg != null && colseMsg.length() >0 ) {
				msg = msg.concat("<br/>"+colseMsg);
			}
			
			
			ResponseMessage message = ResponseMessage.sucMsg(msg);
	
			Context.getTcpServer().broadcastResMsg(message, Constants.COMMRADIO);
		} else {
			if (colseMsg != null && colseMsg.length() >0 ) {
				msg = msg.concat(""+colseMsg);
			}		
			
			ChatMain.systemMessage(msg);
		}
		times++;

		if (delayTime <= 300 && smallRadio == 0) {
			smallRadio = 1;
			GamePowerNoticeHandler noticeHandler = new GamePowerNoticeHandler();
			noticeHandler.smallRadio = 2;
			smallFuture = Context.getTimerServer().scheduleAtFixedRate(noticeHandler, 10);
		}

	}

}
